package com.caiyi.financial.nirvana.sms.service;

import com.caiyi.financial.nirvana.sms.base.service.BaseService;
import com.caiyi.financial.nirvana.sms.entity.SmsMsg;
import com.caiyi.financial.nirvana.sms.enums.ReturnCodeEnum;
import com.caiyi.financial.nirvana.sms.mapper.SmsMsgMapper;
import com.caiyi.financial.nirvana.sms.querybean.MsgDayQueryBean;
import com.caiyi.financial.nirvana.sms.querybean.MsgMonthQueryBean;
import com.caiyi.financial.nirvana.sms.querybean.PageBean;
import com.caiyi.financial.nirvana.sms.querybean.QueryMsgBean;
import com.caiyi.financial.nirvana.sms.util.DateUtil;
import com.caiyi.financial.nirvana.sms.vo.SmsAccountVo;
import com.youyu.common.api.Result;
import com.github.pagehelper.PageHelper;
import com.youyu.common.enums.BaseResultCode;
import com.youyu.common.exception.BizException;
import com.youyu.common.utils.YyAssert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SmsMsgService extends BaseService<SmsMsg> {

    @Autowired
    private SmsMsgMapper smsMsgMapper;

    public PageBean<SmsMsg> getMsgByParams(QueryMsgBean bean) {
        YyAssert.isFalse(null == bean.getStartTime(), ReturnCodeEnum.PARAMS_ERROR.code, "开始时间不能为空");
        if (null == bean.getEndTime()) {
            bean.setEndTime(new Date());
        }
        YyAssert.isFalse(DateUtil.getDateDiffer(bean.getStartTime(), bean.getEndTime()) > 7, ReturnCodeEnum.PARAMS_ERROR.code, "开始时间和结束时间差不能大于7天");
        PageHelper.startPage(bean.getPageNum(), bean.getPageSize());
        List<SmsMsg> list = smsMsgMapper.selectMsgListByParams(bean);
        return new PageBean<>(list);
    }

    public PageBean<SmsAccountVo> getMsgByDayByAccount(MsgDayQueryBean bean) {
        PageHelper.startPage(bean.getPageNum(), bean.getPageSize());
        List<SmsAccountVo> list = smsMsgMapper.selectMsgByDayByAccount(bean);
        return new PageBean<>(list);
    }

    public PageBean<SmsAccountVo> getMsgByMonthByAccount(MsgMonthQueryBean bean) {
        PageHelper.startPage(bean.getPageNum(), bean.getPageSize());
        List<SmsAccountVo> list = smsMsgMapper.selectMsgByMonthByAccount(bean);
        return new PageBean<>(list);
    }

    /**
     * 客户日发送统计缓存表sms_day_account
     */
    public void smsDayAccount() {
        List<String> selectAccount = smsMsgMapper.selectAccount();
        List<String> selectSmsDayAccount = smsMsgMapper.selectSmsDayAccount();
        selectAccount.removeAll(selectSmsDayAccount);
        for (String account : selectAccount) {
            int count = smsMsgMapper.insertSmsDayAccountUser(account);
            logger.info("插入新账号 : {}, count : {}", account, count);
        }
        List<Map> listMap = smsMsgMapper.searchSmsDayAccount();
        int count = 0;
        Map<String, String> param = new HashMap();
        for (Map<String, String> map : listMap) {
            param.put("msgCount", map.get("msgCount"));
            param.put("successCount", map.get("successCount"));
            param.put("unknownCount", map.get("unknownCount"));
            param.put("failureCount", map.get("failureCount"));
            param.put("day", map.get("day"));
            param.put("account", map.get("account"));
            int num = smsMsgMapper.updateSmsDayAccount(param);
            count = num > 0 ? ++count : count;
            logger.info("count : {}", count);
        }
        logger.info("客户日发送统计更新结果 : {}", count);
    }

}
